<template>
  <div></div>
</template>

<script>
export default {
  name: 'MyPhoto',
  data: function () {
    return {
      filter:'image',
      currCamera: null,
    }
  },
  beforeDestroy () {
    this.closeCamera()
  },
  methods: {
    closeCamera: function () {
      if (!this.currCamera || typeof this.currCamera !== 'object' || typeof this.currCamera.close !== 'function') {
        // api.toast({msg: '当前模块未添加，请先升级App'})
        return
      }
      this.currCamera.close()
    },
    chooseAct: function (callback,filter) {
      let $this = this
      if (!filter){
        filter='image'
      }
      $this.filter=filter
      if (localStorage.getItem('allowGalleryCamera') === '1') {

      }

      plus.nativeUI.actionSheet(
        {
          title: '请选择图片来源!',
          cancel: '取消',
          buttons: [
            {
              title: '相册'
            }, {
              title: '相机'
            }
          ]
        },
        function (e) {
          if (e.index === 1) {
            if (localStorage.getItem('allowGallery')==='1'){
              plus.gallery.pick(function (file) {
                callback(file)
              }, function (e) {
                // console.log( "取消选择图片" );
              }, {filter: filter==='all'?'none':filter,multiple:false})
              return;
            }
            $this.$confirm('您是否同意访问您的相册?', '系统提示', {
              customClass: 'my-confirm-box',
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              backType: 'warning'
            }).then(() => {
              localStorage.setItem('allowGallery', '1')
              // 相册选择
              plus.gallery.pick(function (file) {
                callback(file)
              }, function (e) {
                // console.log( "取消选择图片" );
              }, {filter: filter==='all'?'none':filter,multiple:false})
            }).catch(() => {

            })
          }
          if (e.index === 2) {
            if (localStorage.getItem('allowCamera')==="1"){
              $this.openCamera(function (path) {
                callback(path)
              })
              return;
            }
            $this.$confirm('您是否同意访问您的照相机?', '系统提示', {
              customClass: 'my-confirm-box',
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              backType: 'warning'
            }).then(() => {
              localStorage.setItem('allowCamera', '1')
              // 照相机选择
              $this.openCamera(function (path) {
                callback(path)
              })
            }).catch(() => {

            })
            // 手机拍照

          }
          // console.log('User pressed: ' + e.index)
        }
      )

    },
    // 打开系统相机
    openCamera: function (callback) {
      let $this = this
      if (typeof plus !== 'undefined') {
        $this.currCamera = plus.camera.getCamera()
        var res = $this.currCamera.supportedImageResolutions[0]
        var fmt = $this.currCamera.supportedImageFormats[0]
        if ($this.filter==='all'){
          plus.nativeUI.actionSheet(
            {
              title: '请选择图片来源!',
              cancel: '取消',
              buttons: [
                {
                  title: '录像'
                }, {
                  title: '拍照'
                }
              ]
            },
            function (e) {
              // 录像
              if (e.index === 1) {
                var res = $this.currCamera.supportedVideoResolutions[0];
                var fmt = $this.currCamera.supportedVideoFormats[0];
                $this.currCamera.startVideoCapture( function( path ){
                    // alert( "录像成功 success: " + path );
                    callback(path)
                  },
                  function( error ) {
                    // alert( "Capture video failed: " + error.message );
                  },
                  {resolution:res,format:fmt}
                );
              }
              // 拍照
              if (e.index === 2) {
                $this.currCamera.captureImage(function (path) {
                    callback(path)
                    // setTimeout(function () {
                    //   $this.currCamera.close()
                    // }, 1000)
                    // alert( "Capture image success: " + path );
                  },
                  function (error) {
                    plus.nativeUI.toast('取消拍照')
                    // alert('Capture image failed: ' + error.message)
                  },
                  {resolution: res, format: fmt,crop:{quality:20}}
                )
              }
            }
          )
          return
        }
        if ($this.filter==='image'){
          $this.currCamera.captureImage(function (path) {
              callback(path)
              // setTimeout(function () {
              //   $this.currCamera.close()
              // }, 1000)
              // alert( "Capture image success: " + path );
            },
            function (error) {
              plus.nativeUI.toast('取消拍照')
              // alert('Capture image failed: ' + error.message)
            },
            {resolution: res, format: fmt}
          )
        }
      }
    },
  }
}
</script>

<style scoped>

</style>
